import { requestAndroidPermission, gotoAppPermissionSetting } from '@/js_sdk/wa-permission/permission.js'
let dialogView = null;
const list = [
	{
		key: 'ACCESS_FINE_LOCATION',
		title: '位置权限申请说明:',
		content: '为了提供最准确的本地化服务，例如附近地点推荐或基于位置的内容定制，我们需要获取您的地理位置信息。',
		top: 30,
		height: 60
	},
	{
		key: 'CAMERA',
		title: '相机权限申请说明:',
		content: '为了让您能够拍摄照片或扫描二维码，我们的应用程序需要访问您的设备相机。我们仅在您主动选择使用相关功能时才会请求访问，并且会严格遵守隐私政策保护您的个人信息安全。',
		top: 30,
		height: 85
	},
	{
		key: 'READ_EXTERNAL_STORAGE',
		title: '相册权限申请说明:',
		content: '为了让您能够轻松地从相册中选择照片进行上传和分享，我们需要获取访问您相册的权限。通过授予我们访问相册的权限，您可以直接从自己的图片库中挑选照片来个性化您的体验。',
		top: 30,
		height: 85
	},
]
export const setDialogView = (permissionID) => {
	let bool = true;
	const systemInfo = uni.getSystemInfoSync();
	const statusBarHeight = systemInfo.statusBarHeight;
	const totalHeight = statusBarHeight;
	const item = list[list.map(item => item.key).indexOf(permissionID)];
	console.log(permissionID, 'permissionID')
	return new Promise((resolve, reject) => {
		if (item.isRefuse) {
			resolve(-1)
			return
		}
		console.log(['android.permission.' + permissionID])
		requestAndroidPermission(['android.permission.' + permissionID]).then((res) => {
			//1同意 -1拒绝 
			bool = false;
			if (dialogView) {
				dialogView.close()
			}
			resolve(res)
			if (res == -1) {
				item.isRefuse = true;
			}
		})
		setTimeout(() => {
			if (bool) {
				dialogView = new plus.nativeObj.View('per-modal', {
					top: '5px',
					left: '0px',
					width: '100%',
					// backgroundColor: '#444',
					// opacity: 0.5
				})
				dialogView.drawRect({
					color: '#fff',
					radius: '5px'
				}, {
					top: totalHeight + 'px',
					left: '3%',
					width: '94%',
					height: (item.height + 40) + 'px',
				})
				dialogView.drawText(item.title, {
					top: totalHeight + 3 + 'px',
					left: "8%",
					height: "30px"
				}, {
					align: "left",
					color: "#000",
				})
				dialogView.drawText(item.content, {
					top: totalHeight + item.top + 'px',
					height: item.height + 'px',
					left: "8%",
					width: "84%"
				}, {
					whiteSpace: 'normal',
					size: "14px",
					align: "left",
					color: "#656563"
				})
				dialogView.show()
			}
		}, 1000)
	})


}

//清除弹框
const clearDialogView = (state) => {
	console.log('清除弹框样式');
	dialogView && dialogView.close()
	dialogView = null
}